Public Class COrderItem
    Inherits BOBase

    Public Const CONST_UNITED_STATES_COUNTRY As String = "United States"

    Protected m_State As OrderItemDS
    Protected m_DA As OrderItems
    Private m_userBarCode As String
    Private m_userCountry As String
    Private m_objProductCode As CProductCode

#Region "   Inherits Implementation"
    Sub New()
        m_State = New OrderItemDS
        m_DA = New OrderItems
        'Throw New ApplicationException("Object does not support this method")
    End Sub

    Sub New(ByVal state As OrderItemDS, ByVal rowIndex As Integer, ByVal userBarCode As String, ByVal userCountry As String)
        m_State = state
        m_DA = New OrderItems
        m_userBarCode = userBarCode
        m_userCountry = userCountry
        MyBase.m_RowIndex = rowIndex
    End Sub

    Public Overrides ReadOnly Property DA() As DABase
        Get
            If m_DA Is Nothing Then
                m_DA = New OrderItems
            End If

            Return m_DA
        End Get
    End Property

    Public Overrides Property ObjectState() As DataSet
        Get
            Return m_State
        End Get
        Set(ByVal Value As DataSet)
            Try
                m_State = CType(Value, OrderItemDS)
            Catch ex As InvalidCastException
                Throw New ApplicationException("Invalid ORDER_ITEM Dataset!", ex)
            End Try
        End Set
    End Property

    Public Overrides Sub Init()
        Dim dr As OrderItemDS.ORDER_ITEMRow = m_State.ORDER_ITEM.NewORDER_ITEMRow
        With dr
            .ORDER_ID_FK = Nothing
            .PRODUCT_CODE_ID_FK = Nothing
            .COLOR_ID_FK = Nothing
            .PACKING = Nothing
            .QUANTITY = Nothing
            .IS_FIXED = Nothing
            .SetSPECIAL_INSTRUCTIONSNull()
            .SetUSE_FROM_BO_ITEMNull()
            .SetADJUSTED_QUANTITYNull()
        End With
        m_State.ORDER_ITEM.Rows.Add(dr)
    End Sub
#End Region

#Region "   Data Properties"
    Public ReadOnly Property ProductCodeTable() As CProductCode
        Get
            If m_objProductCode Is Nothing Then
                m_objProductCode = New CProductCode
                m_objProductCode.Load(Me.ProductCodeID)
            End If

            Return m_objProductCode
        End Get
    End Property

    Protected ReadOnly Property DataRow() As OrderItemDS.ORDER_ITEMRow
        Get
            Return CType(Me.Row, OrderItemDS.ORDER_ITEMRow)
        End Get
    End Property

    Public ReadOnly Property ID() As System.Int32
        Get
            Return Me.DataRow.OI_ID_PK
        End Get
    End Property

    Public Property OrderId() As System.Int32
        Get
            Return Me.DataRow.ORDER_ID_FK
        End Get
        Set(ByVal Value As System.Int32)
            Me.DataRow.ORDER_ID_FK = Value
        End Set
    End Property

    Public ReadOnly Property ProductCode() As System.String
        Get
            Try
                Return Me.ProductCodeTable.Code
            Catch ex As Exception
            End Try
        End Get
    End Property

    Public Property ProductCodeID() As System.Int32
        Get
            Return Me.DataRow.PRODUCT_CODE_ID_FK
        End Get
        Set(ByVal Value As System.Int32)
            'Me.DataRow.PRODUCT_CODE_ID_FK = Value

            ' MHoa added. Re-load the ProductCodeTable to reflect the changed ProductCodeID
            If Me.DataRow.PRODUCT_CODE_ID_FK <> Value Then
                Me.DataRow.PRODUCT_CODE_ID_FK = Value
                Me.ProductCodeTable.Load(Value)
            End If
        End Set
    End Property

    Public ReadOnly Property ProductDescription() As System.String
        Get
            Try
                Return Me.ProductCodeTable.Description
            Catch ex As Exception
                Return ""
            End Try
        End Get
    End Property

    'Public ReadOnly Property MimsProductCode() As System.String
    '    Get
    '        Return Common.GetLKDValue(Me.MimsProductCodeLkdID, 1)
    '    End Get
    'End Property

    'Public Property MimsProductCodeLkdID() As System.Int32
    '    Get
    '        Return Me.DataRow.MIMS_PRODUCT_CODE_ID_FK
    '    End Get
    '    Set(ByVal Value As System.Int32)
    '        Me.DataRow.MIMS_PRODUCT_CODE_ID_FK = Value
    '    End Set
    'End Property

    'Public ReadOnly Property Color() As System.String
    '    Get
    '        Return Common.GetLKDValue(Me.ColorLkdID, 1)
    '    End Get
    'End Property

    'Public Property ColorLkdID() As System.Int32
    '    Get
    '        Return Me.DataRow.COLOR_ID_FK
    '    End Get
    '    Set(ByVal Value As System.Int32)
    '        Me.DataRow.COLOR_ID_FK = Value
    '    End Set
    'End Property

    Public ReadOnly Property Color() As System.String
        Get
            If Me.DataRow.IsCOLOR_ID_FKNull Then
                Return Nothing
            Else
                Return Common.GetLKDValue(Me.ColorLkdID, 1)
            End If
        End Get
    End Property

    Public Property ColorLkdID() As System.Int32
        Get
            If Me.DataRow.IsCOLOR_ID_FKNull Then
                Return Nothing
            Else
                Return Me.DataRow.COLOR_ID_FK
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetCOLOR_ID_FKNull()
            Else
                Me.DataRow.COLOR_ID_FK = Value
            End If
        End Set
    End Property

    Public Property Packing() As System.Byte
        Get
            Return Me.DataRow.PACKING
        End Get
        Set(ByVal Value As System.Byte)
            Me.DataRow.PACKING = Value
        End Set
    End Property

    Public Property Quantity() As System.Int32
        Get
            Return Me.DataRow.QUANTITY
        End Get
        Set(ByVal Value As System.Int32)
            Me.DataRow.QUANTITY = Value
        End Set
    End Property

    Public Property IsFixed() As System.Boolean
        Get
            Return Me.DataRow.IS_FIXED
        End Get
        Set(ByVal Value As System.Boolean)
            Me.DataRow.IS_FIXED = Value
        End Set
    End Property

    Public Property SpecialInstructions() As System.String
        Get
            If Me.DataRow.IsSPECIAL_INSTRUCTIONSNull Then
                Return Nothing
            Else
                Return Me.DataRow.SPECIAL_INSTRUCTIONS
            End If
        End Get
        Set(ByVal Value As System.String)
            If Value = Nothing Then
                Me.DataRow.SetSPECIAL_INSTRUCTIONSNull()
            Else
                Me.DataRow.SPECIAL_INSTRUCTIONS = Value
            End If
        End Set
    End Property

    Public Property UseFromBoItem() As System.Int32
        Get
            If Me.DataRow.IsUSE_FROM_BO_ITEMNull Then
                Return Nothing
            Else
                Return Me.DataRow.USE_FROM_BO_ITEM
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetUSE_FROM_BO_ITEMNull()
            Else
                Me.DataRow.USE_FROM_BO_ITEM = Value
            End If
        End Set
    End Property

    Public Property AdjustedQuantity() As System.Int32
        Get
            If Me.DataRow.IsADJUSTED_QUANTITYNull Then
                Return Nothing
            Else
                Return Me.DataRow.ADJUSTED_QUANTITY
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetADJUSTED_QUANTITYNull()
            Else
                Me.DataRow.ADJUSTED_QUANTITY = Value
            End If
        End Set
    End Property

    'Public Property AdjustedByIdFk() As System.Int32
    '    Get
    '        If Me.DataRow.IsADJUSTED_BY_ID_FKNull Then
    '            Return Nothing
    '        Else
    '            Return Me.DataRow.ADJUSTED_BY_ID_FK
    '        End If
    '    End Get
    '    Set(ByVal Value As System.Int32)
    '        If Value = Nothing Then
    '            Me.DataRow.SetADJUSTED_BY_ID_FKNull()
    '        Else
    '            Me.DataRow.ADJUSTED_BY_ID_FK = Value
    '        End If
    '    End Set
    'End Property

    'Public Property AdjustedDate() As System.DateTime
    '    Get
    '        If Me.DataRow.IsADJUSTED_DATENull Then
    '            Return Nothing
    '        Else
    '            Return Me.DataRow.ADJUSTED_DATE
    '        End If
    '    End Get
    '    Set(ByVal Value As System.DateTime)
    '        If Value = Nothing Then
    '            Me.DataRow.SetADJUSTED_DATENull()
    '        Else
    '            Me.DataRow.ADJUSTED_DATE = Value
    '        End If
    '    End Set
    'End Property

    Public ReadOnly Property IsQuantityAdjusted() As Boolean
        Get
            Return Not Me.DataRow.IsADJUSTED_QUANTITYNull
        End Get
    End Property

    'Public Property AdjustedAprrovedByIdFk() As System.Int32
    '    Get
    '        If Me.DataRow.IsADJUSTED_APRROVED_BY_ID_FKNull Then
    '            Return Nothing
    '        Else
    '            Return Me.DataRow.ADJUSTED_APRROVED_BY_ID_FK
    '        End If
    '    End Get
    '    Set(ByVal Value As System.Int32)
    '        If Value = Nothing Then
    '            Me.DataRow.SetADJUSTED_APRROVED_BY_ID_FKNull()
    '        Else
    '            Me.DataRow.ADJUSTED_APRROVED_BY_ID_FK = Value
    '        End If
    '    End Set
    'End Property

    'Public Property ApprovalDate() As System.DateTime
    '    Get
    '        If Me.DataRow.IsAPPROVAL_DATENull Then
    '            Return Nothing
    '        Else
    '            Return Me.DataRow.APPROVAL_DATE
    '        End If
    '    End Get
    '    Set(ByVal Value As System.DateTime)
    '        If Value = Nothing Then
    '            Me.DataRow.SetAPPROVAL_DATENull()
    '        Else
    '            Me.DataRow.APPROVAL_DATE = Value
    '        End If
    '    End Set
    'End Property

    Public ReadOnly Property UseFromBoNumber() As String
        Get
            If Me.DataRow.IsUseFromBoNumberNull Then
                Return "N/A"
            Else
                Return Me.DataRow.UseFromBoNumber
            End If
        End Get
    End Property

    Public ReadOnly Property UseFromBoNumber2() As String
        Get
            If Me.DataRow.IsUSE_FROM_BO_ITEMNull OrElse Not Me.UseFromBoItem > 0 Then
                Return "N/A"
            Else
                Try
                    Dim objOI As New COrderItem
                    objOI.Load(Me.UseFromBoItem)

                    Dim obj As New COrder
                    obj.Load(objOI.OrderId)
                    Return obj.OrderNumber
                Catch ex As Exception
                    Return "Failed to loaded"
                End Try

            End If
        End Get
    End Property

    ' Bar Code for USA users
    Private Shared ReadOnly Property UPC(ByVal upcHead As String, ByVal sProductCode As String) As String
        Get
            If sProductCode Is Nothing Then
                Return String.Empty
            End If

            Dim upcDigits As String = upcHead.Replace(" ", String.Empty)
            upcDigits = upcDigits & sProductCode

            Dim i As Integer

            ' Step 1: add all characters in the odd positions (left to right)
            Dim res As Integer
            res = 0
            For i = 0 To upcDigits.Length - 1 Step 2
                res = res + CInt(upcDigits.Chars(i).ToString())
            Next

            ' Step 2: multiply the result obtained in Step 1 by 3
            res = res * 3

            ' Step 3: sum of all characters in the even positions
            Dim res3 As Integer
            res3 = 0
            For i = 1 To upcDigits.Length - 1 Step 2
                res3 = res3 + CInt(upcDigits.Chars(i).ToString())
            Next

            ' Step 4: add the product of Step 2 to the sum of Step 3
            res = res + res3

            ' Step 5: find check charater, the smallest number which,
            '           when added to the sum of Step 4, produces a multiple of 10.
            Dim checkNumber As Integer
            checkNumber = 10 - res Mod 10

            ' mod 10
            checkNumber = checkNumber Mod 10

            Return sProductCode & " " & checkNumber
            'Return upcHead & " " & sProductCode & " " & checkNumber
        End Get
    End Property

    ' Bar Code for non-USA users
    Private Shared ReadOnly Property EAN(ByVal eanHead As String, ByVal sProductCode As String) As String
        Get
            If sProductCode Is Nothing Then
                Return String.Empty
            End If

            Dim eanDigits As String = eanHead.Replace(" ", String.Empty)
            eanDigits = eanDigits & sProductCode

            Dim i As Integer

            ' Step 1: add all characters in the odd positions (right to left, the right most charater is odd)
            Dim res As Integer
            res = 0
            For i = eanDigits.Length - 1 To 0 Step -2
                res = res + CInt(eanDigits.Chars(i).ToString())
            Next

            ' Step 2: multiply the result obtained in Step 1 by 3
            res = res * 3

            ' Step 3: sum of all characters in the even positions
            Dim res3 As Integer
            res3 = 0
            For i = eanDigits.Length - 2 To 0 Step -2
                res3 = res3 + CInt(eanDigits.Chars(i).ToString())
            Next

            ' Step 4: add the product of Step 2 to the sum of Step 3
            res = res + res3

            ' Step 5: find check charater, the smallest number which,
            '           when added to the sum of Step 4, produces a multiple of 10.
            Dim checkNumber As Integer
            checkNumber = 10 - res Mod 10

            ' mod 10
            checkNumber = checkNumber Mod 10

            Return sProductCode & " " & checkNumber
            'Return eanHead & " " & sProductCode & " " & checkNumber
        End Get
    End Property

    Public ReadOnly Property BarCode() As String
        Get
            If (Me.ProductCodeTable.PartOfBarcode Is Nothing OrElse Me.ProductCodeTable.PartOfBarcode.Length <= 0) Then
                Return "N/A"
            End If

            If Me.m_userCountry = CONST_UNITED_STATES_COUNTRY Then
                Return Me.UPC(m_userBarCode, Me.ProductCodeTable.PartOfBarcode)
            Else
                Return Me.EAN(m_userBarCode, Me.ProductCodeTable.PartOfBarcode)
            End If
        End Get
    End Property

    Public Shared Function GetBarCode(ByVal isUPC As Boolean, ByVal sHead As String, ByVal sProductCode As String) As String
        If isUPC Then
            Return UPC(sHead, sProductCode)
        Else
            Return EAN(sHead, sProductCode)
        End If
    End Function
#End Region

End Class
